package nero.yncc.com.createart.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.adapter.AdapterIndexChangjing;
import nero.yncc.com.createart.adapter.AdapterIndexChangjingList;
import nero.yncc.com.createart.adapter.AdapterIndexHotSale;
import nero.yncc.com.createart.adapter.AdapterIndexNewGod;
import nero.yncc.com.createart.base.BaseFragment;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.ChangjingBean;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.utils.HttpUtils;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * Created by nero on 2017/7/11.
 */

public class FragmentCollection extends BaseFragment {

    public final static int STATE_GOODS = 0, STATE_CHANGJING = 1;

    PullToRefreshListView pl_list;
    ArrayList<GodBean.Goods> arrayList;
    ArrayList<ChangjingBean> arrayList_changjing;

    AdapterIndexHotSale adapterIndexNewGod;
    AdapterIndexChangjing adapterIndexChangjingList;

    LinearLayout ll_nodata;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.single_pulltorefresh_notitle, null);
        pl_list = view.findViewById(R.id.pl_list);
        ll_nodata = view.findViewById(R.id.ll_nodata);
        pl_list.setMode(PullToRefreshBase.Mode.BOTH);
        state = getArguments().getInt("type");
        initAll();
        return view;
    }

    int state = STATE_CHANGJING;
    boolean canLoad = true;

    @Override
    protected void initData() {
        super.initData();
        arrayList = new ArrayList<>();
        arrayList_changjing = new ArrayList<>();
        if (state == STATE_GOODS) {
            adapterIndexNewGod = new AdapterIndexHotSale(getContext(), arrayList);
            pl_list.setAdapter(adapterIndexNewGod);
        } else {
            adapterIndexChangjingList = new AdapterIndexChangjing(getContext(), arrayList_changjing);
            pl_list.setAdapter(adapterIndexChangjingList);
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        pl_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                canLoad = true;
                if (state == STATE_GOODS) {
                    arrayList.clear();
                } else {
                    arrayList_changjing.clear();
                }
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getData();
            }
        });
       if(state==STATE_GOODS && arrayList.size()==0){
           pl_list.setRefreshing();
       }
//       if(state==STATE_CHANGJING && arrayList_changjing.size()==0){
//           pl_list.setRefreshing();
//       }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if(state==STATE_GOODS && arrayList!=null && arrayList.size()==0){
                pl_list.setRefreshing();
            }
            if(state==STATE_CHANGJING && arrayList_changjing!=null && arrayList_changjing.size()==0){
                pl_list.setRefreshing();
            }
        }
    }

    int pageSize = 10;
    int page = 1;

    @Override
    public void getData() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("user_id", userBean.getUser_id());
        switch (state) {
            case STATE_GOODS:
                hashMap.put("action", "GOODS");
                break;
            case STATE_CHANGJING:
                hashMap.put("action", "CHANGJING");
                break;
        }
        hashMap.put("pagesize", pageSize + "");
        hashMap.put("pageindex", page + "");

        new HttpUtils().post(getActivity(), Constants.API_COLLECTION_LIST, hashMap, null, HttpUtils.TYPE_ARRAY, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                JSONArray jsonArray = (JSONArray) json;
                if (!canLoad) {
                    pl_list.onRefreshComplete();
                    if (page > 1)
                        showToast("没有更多内容啦");
                    return;
                }
                if (page == 1 && jsonArray.length() == 0) {
                    pl_list.setVisibility(GONE);
                    ll_nodata.setVisibility(VISIBLE);
                } else {
                    pl_list.setVisibility(VISIBLE);
                    ll_nodata.setVisibility(View.GONE);
                }
                if (jsonArray.length() < pageSize) {
                    canLoad = false;
                }
                for (int a = 0; a < jsonArray.length(); a++) {
                    try {
                        switch (state) {
                            case STATE_GOODS:
                                arrayList.add(JSON.parseObject(jsonArray.getString(a), GodBean.class).getGoods());
                                break;
                            case STATE_CHANGJING:
                                arrayList_changjing.add(JSON.parseObject(jsonArray.getString(a), ChangjingBean.class));
                                break;
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                switch (state) {
                    case STATE_GOODS:
                        adapterIndexNewGod.notifyDataSetChanged();
                        break;
                    case STATE_CHANGJING:
                        adapterIndexChangjingList.notifyDataSetChanged();
                        break;
                }
                pl_list.onRefreshComplete();
            }

            @Override
            public void fail(String failMsg) {
                showToast(failMsg);
                pl_list.onRefreshComplete();
            }
        });
    }
}
